package at.ac.univie.zsu.aguataplan.domain;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.format.annotation.DateTimeFormat;

/**
 * @author gerry
 * 
 */
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Person {

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	@Column
	private Long id;

	@Version
	@Column
	private Integer version;

	@NotNull
	@Size(min = 2, max = 30)
	@Column
	private String firstName;

	@NotNull
	@Size(min = 2, max = 30)
	@Column
	private String lastName;

	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(style = "M-")
	@Column
	private Date birthday;

	@Size(min = 2, max = 30)
	@Column
	private String phone;

	@NotNull
	@Size(min = 1, max = 1)
	@Column
	private String sex;

	@OneToMany(cascade = CascadeType.ALL)
	private Set<PersonEMail> refPersonEMail = new HashSet<PersonEMail>();

	@OneToMany(cascade = CascadeType.ALL)
	private Set<PersonWeb> refPersonWeb = new HashSet<PersonWeb>();

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public Person() {

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Birthday: ").append(getBirthday()).append(", ");
		sb.append("FirstName: ").append(getFirstName()).append(", ");
		sb.append("Id: ").append(getId()).append(", ");
		sb.append("LastName: ").append(getLastName()).append(", ");
		sb.append("Phone: ").append(getPhone()).append(", ");
		sb.append("RefPersonEMail: ")
				.append(getRefPersonEMail() == null ? "null"
						: getRefPersonEMail().size()).append(", ");
		sb.append("RefPersonWeb: ")
				.append(getRefPersonWeb() == null ? "null" : getRefPersonWeb()
						.size()).append(", ");
		sb.append("Sex: ").append(getSex()).append(", ");
		sb.append("Version: ").append(getVersion());
		return sb.toString();
	}

	// ----------------------------------------------------
	// GETTER & SETTER
	// ----------------------------------------------------

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Set<PersonEMail> getRefPersonEMail() {
		return refPersonEMail;
	}

	public void setRefPersonEMail(Set<PersonEMail> refPersonEMail) {
		this.refPersonEMail = refPersonEMail;
	}

	public Set<PersonWeb> getRefPersonWeb() {
		return refPersonWeb;
	}

	public void setRefPersonWeb(Set<PersonWeb> refPersonWeb) {
		this.refPersonWeb = refPersonWeb;
	}

}
